//
//  UUOrderDetailVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 22/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUOrderDetailVC.h"
#import "UUOrderManager.h"
#import "UUContentContainerVC.h"
#import "UUOrderDetailBasicInfoVC.h"
#import "UUOrderDetailAssignInfoVC.h"
#import "UUOrderDetailConfirmVC.h"
#import "UUOrderDetailRateVC.h"

@interface UUOrderDetailVC () <UUOrderManagerObserver>

@property (nonatomic, strong) UUOrderDetailModel *order;
@property (nonatomic, strong) UUContentContainerVC *contentContainerVC;

@end

@implementation UUOrderDetailVC

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        self.title = @"订单详情";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DKAssert(self.orderId != nil);
    
    self.contentContainerVC = [UUContentContainerVC instanceOfAppropriate];
    [self addChildViewController:self.contentContainerVC];
    [self.view addSubview:self.contentContainerVC.view];
    [self.contentContainerVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self reload];
    
    [GetOrderManager() addObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reload {
    ShowIndicator();
    WEAK_SELF();
    [GetOrderManager() asyncFetchOrderDetailWithOrderId:self.orderId completeBlock:^(UUOrderDetailModel *order, NSError *error) {
        HideIndicatorInView(weakSelf.view);
        if (error == nil) {
            weakSelf.order = order;
            [weakSelf.contentContainerVC removeAllChildrenVC];
            [weakSelf setupUI];
        } else {
            ShowTipsOnView(error.localizedDescription, weakSelf.view);
        }
    }];
}

- (void)setupUI {
    UUOrderDetailBasicInfoVC *basicInfoVC = [UUOrderDetailBasicInfoVC instanceOfAppropriate];
    basicInfoVC.order = self.order;
    [self.contentContainerVC insertChildVC:basicInfoVC];
    
    if ([self needsToShowAssignInfo]) {
        UUOrderDetailAssignInfoVC *assignInfoVC = [UUOrderDetailAssignInfoVC instanceOfAppropriate];
        assignInfoVC.order = self.order;
        [self.contentContainerVC insertChildVC:assignInfoVC];
    }
    
    if (self.order.status == UUOrderStatusWaitingForConfirm) {
        UUOrderDetailConfirmVC *confirmVC = [UUOrderDetailConfirmVC instanceOfAppropriate];
        confirmVC.orderId = self.orderId;
        [self.contentContainerVC insertChildVC:confirmVC];
    } else if (self.order.status == UUOrderStatusComplete && self.order.score > 0) {
        UUOrderDetailRateVC *rateVC = [UUOrderDetailRateVC instanceOfAppropriate];
        rateVC.order = self.order;
        [self.contentContainerVC insertChildVC:rateVC];
    }
}

- (BOOL)needsToShowAssignInfo {
    return self.order.status == UUOrderStatusWaitingForService
    || self.order.status == UUOrderStatusInService
    || self.order.status == UUOrderStatusComplete
    || self.order.refundStatus == UUOrderRefundStatusInRefunding
    || self.order.status == UUOrderStatusRefunded
    || self.order.status == UUOrderStatusCancelled
    || self.order.status == UUOrderStatusWaitingForAssign;
}

#pragma mark - UUOrderManagerObserver methods

- (void)orderManagerDidUpdateOrders:(NSArray<id> *)orders {
    if ([orders containsObject:self.orderId]) {
        [self reload];
    }
}

@end
